let express = require("express");
let app = express();
let path = require("path");

//处理body请求体的中间件
app.use(express.urlencoded());
app.use(express.json());

//查询学生接口
app.get("/user", (request, response) => {
  const { userid } = request.query;

  //查询失败
  if (userid !== '1') {
    return response.send({
      code: 1,
      msg: "查询失败",
      data: null,
    });
  }

  //查询成功
  response.send({
    code: 10000,
    msg: "查询成功",
    data: {
        name: "李博",
        age: 18,
        sex: "男"
      },
  });
});

app.post('/post',(request, response)=>{
    console.log('post');
    console.log(request.body, "req.body");
    console.log(request.query, "req.query");
    let {name,age,sex} = request.body
    if (!name||!age||!sex) {
        return response.send({
            code:10001,
            msg: "查询失败",
            data: null,
        })
    }
    response.send({
        code: 10000,
        mes: "添加成功",
        data: null,
    })
})

app.put('/put',(request, response)=>{
    let {name,age,sex} = request.body
    response.send({
        code: 10000,
        mes: "更新成功",
        data: null
    })
})

app.delete('/delete',(request, response)=>{
    let {userid} = request.query
    response.send({
        code: 10000,
        mes: "删除成功",
        data: null
    })
})

//静态资源部署接口
app.get("/1", (request, response) => {
  let filepath = path.resolve(__dirname, "./axios.html");
  response.sendFile(filepath);
});

app.get('/2',(request,response)=>{
    let filepath = path.resolve(__dirname,'./axios请求别名.html')
    response.sendFile(filepath);
})
app.listen(8080, () => {
  console.log("running");
});
